//
//  ContentInfoCell.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/10.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "ContentInfoCell.h"
#import "CustomLayout.h"

@interface ContentInfoCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *memberButton;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, assign) NSInteger picNumber;
@end

@implementation ContentInfoCell

- (UICollectionView *)collection {
    if (!_collection) {
        CustomLayout *flowLayout = [[CustomLayout alloc] init];
        flowLayout.maximumSpacing = 10;
        flowLayout.itemSize = CGSizeMake(kCellSize, kCellSize);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(13, 15, 25, 15);
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.scrollEnabled = NO;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.pagingEnabled = YES;
        _collection.backgroundColor = [UIColor whiteColor];
        [self addSubview:_collection];
        
        [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"picCell"];
    }
    return _collection;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createView];
    }
    return self;
}

- (void)createView {
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 5;
    [self addSubview:_bgView];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(4, 10, 4, 10));
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.left.mas_greaterThanOrEqualTo(self.bgView).with.offset(10);
        make.right.mas_lessThanOrEqualTo(self.bgView).with.offset(-10);
        make.top.mas_equalTo(self.bgView).with.offset(20);
    }];
    
    _memberButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_memberButton setTitle:@"会员" forState:UIControlStateNormal];
    _memberButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_memberButton setTitleColor:[UIColor colorWithHexString:@"ffa631"] forState:UIControlStateNormal];
    _memberButton.layer.borderColor = [UIColor colorWithHexString:@"ffa631"].CGColor;
    _memberButton.layer.borderWidth = .5f;
    _memberButton.layer.cornerRadius = 3;
    [self addSubview:_memberButton];
    
    [_memberButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(17);
        make.left.mas_equalTo(self.bgView).with.offset(15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(15);
    }];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = [UIFont systemFontOfSize:14];
    _timeLabel.textColor = [UIColor colorWithHexString:@"999999"];
    _timeLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_timeLabel];

    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.memberButton);
        make.right.mas_equalTo(self.bgView).with.offset(-15);
        make.left.mas_greaterThanOrEqualTo(self.memberButton.mas_right).with.offset(10);
    }];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:16];
    _contentLabel.textColor = [UIColor colorWithHexString:@"666666"];
    _contentLabel.numberOfLines = 0;
    [self addSubview:_contentLabel];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView).with.offset(15);
        make.right.mas_equalTo(self.bgView).with.offset(-15);
        make.top.mas_equalTo(self.memberButton.mas_bottom).with.offset(20);
    }];

    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.mas_equalTo(self.bgView);
        make.top.mas_equalTo(self.contentLabel.mas_bottom);
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picNumber;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    cell.layer.cornerRadius = 2;
    
    return cell;
}

- (void)updateUIFromData:(NewsModel *)model {
    _titleLabel.text = [NSString stringWithFormat:@"%@", model.title];
    _timeLabel.text = [NSString stringWithFormat:@"%@", model.time];
    _contentLabel.attributedText = model.attrContent;
    _picNumber = model.pictureNumber;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
